package cn.gp.chatclient;

import cn.gp.chatclient.api.user.UserApi;
import cn.gp.chatclient.config.ClientConfig;
import cn.gp.chatclient.service.UserService;
import cn.gp.chatclient.ui.frame.LoginDialog;
import cn.gp.chatclient.ui.frame.MainFrame;
import cn.gp.chatclient.ui.frame.RegisterDialog;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

import javax.swing.*;
import java.awt.*;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName ClientMain
 * @Description TODO
 * @Author He Guoping
 * @Date 2025/6/17 9:20
 * @Version JDK17
 */

public class ClientMain {
    private static UserService userService;
    private static final ClientConfig config = ClientConfig.getInstance();

    public static void main(String[] args) {
        // 检查服务器连接
        if (!ClientConfig.checkServerConnection()) {
            JOptionPane.showMessageDialog(null,
                "无法连接到服务器，请检查服务器是否启动",
                "连接错误",
                JOptionPane.ERROR_MESSAGE);
            return;
        }

        // 初始化HTTP客户端
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ClientConfig.HTTP_BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(JacksonConverterFactory.create())
                .build();

        // 创建UserApi实例
        UserApi userApi = retrofit.create(UserApi.class);
        userService = new UserService(userApi);

        setupLookAndFeel();
        showSelectionDialog();
    }

    private static void setupLookAndFeel() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            JFrame.setDefaultLookAndFeelDecorated(true);
            JDialog.setDefaultLookAndFeelDecorated(true);
        } catch (Exception e) {
            System.err.println("无法设置系统外观:");
            e.printStackTrace();
        }
    }

    public static void showSelectionDialog() {
        SwingUtilities.invokeLater(() -> {
            JDialog selectionDialog = new JDialog((JFrame)null, "欢迎", true);
            selectionDialog.setSize(300, 200);
            selectionDialog.setLayout(new BorderLayout());
            selectionDialog.setResizable(false);

            JPanel panel = new JPanel(new GridLayout(2, 1, 10, 10));
            panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

            JButton loginBtn = new JButton("登录");
            loginBtn.setFont(new Font("微软雅黑", Font.BOLD, 14));
            loginBtn.addActionListener(e -> {
                selectionDialog.dispose();
                showLoginDialog();
            });

            JButton registerBtn = new JButton("注册");
            registerBtn.setFont(new Font("微软雅黑", Font.BOLD, 14));
            registerBtn.addActionListener(e -> {
                selectionDialog.dispose();
                showRegisterDialog();
            });

            panel.add(loginBtn);
            panel.add(registerBtn);

            JLabel titleLabel = new JLabel("请选择操作", SwingConstants.CENTER);
            titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
            titleLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));

            selectionDialog.add(titleLabel, BorderLayout.NORTH);
            selectionDialog.add(panel, BorderLayout.CENTER);

            selectionDialog.setLocationRelativeTo(null);
            selectionDialog.setVisible(true);
        });
    }

    private static void showLoginDialog() {
        LoginDialog loginDialog = new LoginDialog(userService, userVo -> {
            // 登录成功回调
            SwingUtilities.invokeLater(() -> {
                MainFrame mainFrame = new MainFrame(userService, userVo);
                mainFrame.setVisible(true);
            });
        });
        loginDialog.setVisible(true);
    }

    private static void showRegisterDialog() {
        RegisterDialog registerDialog = new RegisterDialog(userService, () -> {
            // 注册成功回调
            JOptionPane.showMessageDialog(null,
                    "注册成功，请登录",
                    "提示",
                    JOptionPane.INFORMATION_MESSAGE);
            showLoginDialog();
        });
        registerDialog.setVisible(true);
    }
}